//Copyright 2012 - Jonathan Allen

#if WPF
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Navigation;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Input;
using System.Diagnostics;

namespace Granite.Xaml.Internals
{
	/// <summary>
	/// This INavigator is used for WPF applications that use the NavigationService (e.g. NavigationWindow or Frame)
	/// </summary>
	public class NavigationServiceNavigator : INavigator
	{
		readonly NavigationService m_NavigatorService;

		/// <summary>
		/// Creates a new NavigationServiceNavigator
		/// </summary>
		/// <param name="navigationService"></param>
		public NavigationServiceNavigator(NavigationService navigationService)
		{
			if (navigationService == null)
				throw new ArgumentNullException("navigationService", "navigationService is null.");
			Contract.EndContractBlock();

			m_NavigatorService = navigationService;
		}

		/// <summary>
		/// Navigate to the indicated URI
		/// </summary>
		/// <param name="target"></param>
		public virtual void Navigate(Uri target)
		{
			NavigatorService.Navigate(target);
		}

		///// <summary>
		///// Navigate to the indicated URI
		///// </summary>
		///// <param name="target"></param>
		//public virtual void Navigate(Uri target, object navigationState)
		//{
		//	NavigatorService.Navigate(target, navigationState);
		//}

		///// <summary>
		///// Returns true if the overload of Navigate that includes navigationState can be used.
		///// </summary>
		//public bool SupportsNavigationState
		//{
		//	get { return true; }
		//}

		/// <summary>
		/// Indicates that GoBack can be safely called.
		/// </summary>
		public virtual bool CanGoBack
		{
			get { return NavigatorService.CanGoBack; }
		}

		/// <summary>
		/// Indicates that GoForward can be safely called.
		/// </summary>
		public virtual bool CanGoForward
		{
			get { return NavigatorService.CanGoForward; }
		}

		/// <summary>
		/// Return to the previous View in the navigation journal.
		/// </summary>
		public virtual void GoBack()
		{
			NavigatorService.GoBack();
		}

		/// <summary>
		/// Go to the next view in the navigation journal.
		/// </summary>
		public virtual void GoForward()
		{
			NavigatorService.GoForward();
		}

		/// <summary>
		/// Access the underlying NavigatorService for this Navigator
		/// </summary>
		public NavigationService NavigatorService
		{
			get
			{
				Contract.Ensures(Contract.Result<NavigationService>() != null);
				return m_NavigatorService;
			}
		}

		[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
		[ContractInvariantMethod]
		void ContractInvariant()
		{
			Contract.Invariant(m_NavigatorService != null);
		}



		/// <summary>
		/// Opens a website.
		/// </summary>
		/// <param name="target"></param>
        public virtual void OpenWebsite(Uri target)
		{
			if (target == null)
				throw new ArgumentNullException("target", "target is null.");
			Contract.EndContractBlock();

			Contract.Assume(!string.IsNullOrEmpty(target.ToString()));
			try
			{
				Mouse.OverrideCursor = Cursors.AppStarting;
				Process.Start(target.ToString());
			}
			finally
			{
				Mouse.OverrideCursor = null;
			}
		}


		/// <summary>
		/// Indicates whether or not websites are included in the journal. Always false.
		/// </summary>
        public bool IncludeWebInNavigation
		{
			get { return false; }
		}
	}

}
#endif
